<template>
  <el-menu 
    :style="{width: isCollapse ? '64px' : '200px'}"
    active-text-color="#ffd04b" 
    background-color="#545c64" 
    class="aside" 
    text-color="#fff" 
    @open="handleOpen" 
    @close="handleClose"
    :collapse="isCollapse"
    :default-active="active"
    >
    <p class="title">{{ isCollapse ? 'HO' : 'Hospital System' }}</p>
    <TreeMenu :menu-data="menuData" />
  </el-menu>
</template>

<script setup>
import TreeMenu from './treeMenu.vue'
import { computed, reactive,watch} from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex';

const store = useStore();

const route = useRoute();
const active = computed(() => store.state.menu.menuActive)

// 监听路由变化，同步更新菜单激活状态
watch(
  () => route.path,
  (newPath) => {
    if (newPath && newPath !== '/') {
      store.commit('updateMenuActive', newPath);
    }
  },
  { immediate: true } // 立即执行一次，处理初始加载情况
);


const menuData = computed(() => store.state.menu.routerList)


const isCollapse = computed(() => store.state.menu.isCollapse)

const handleOpen = () => {
}

const handleClose = () => {
}
</script>

<style lang="less" scoped>
.aside {
  height: 100vh;
}
.title {
  color: #fff;
  text-align: center;
  font-size: 20px;
  padding: 20px 0;
  margin: 0;
}
</style>